//
//  LYWithDrawListController.m
//  Btpin
//
//  Created by liuyan on 2019/4/15.
//  Copyright © 2019年 Vic. All rights reserved.
//

#import "LYWithDrawListController.h"
#import "LYWithDrawListCell.h"

@interface LYWithDrawListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger page;

@end

@implementation LYWithDrawListController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(247, 247, 247);
    self.title = @"提现记录";
    self.page = 1;
    [self createSubview];
    [self requestData];
    [self pullUpRefresh];
}

- (void)createSubview{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT-HeightOfStatusBarAndNavigationBar) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = RGB(247, 247, 247);
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    
    [self.tableview registerClass:[LYWithDrawListCell class] forCellReuseIdentifier:kCellIdentifier_LYWithDrawListCell];
    [self.view addSubview:self.tableview];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYWithDrawListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYWithDrawListCell forIndexPath:indexPath];
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

#pragma mark-- loadData
- (void)pullUpRefresh{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullDown)];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.textColor = RGB(85, 85, 85);
    [header setTitle:@"下拉刷新..." forState:MJRefreshStateIdle];
    [header setTitle:@"松手刷新..." forState:MJRefreshStatePulling];
    [header setTitle:@"拼命加载中..." forState:MJRefreshStateRefreshing];
    self.tableview.mj_header = header;
    
    //    //上啦加载
    __weak typeof(self) weakSelf = self;
    MJRefreshAutoNormalFooter *footer =  [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf pullUp];
    }];
    footer.stateLabel.textColor = RGB(153, 153, 153);
    self.tableview.mj_footer = footer;
}

/**
 下拉
 */
- (void)pullDown{
    self.page = 1;
    [self requestData];
}

- (void)pullUp{
    self.page ++;
    [self requestData];
}

- (void)requestData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    dic[@"status"] = @0;
    dic[@"page"] = [NSNumber numberWithInteger:self.page];
    dic[@"number"] = @10;
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Wallet_getWithdrawLogs_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            //收入信息信息
            [weakSelf organizeData:responseObject[@"data"]];
        }else{
            SVPHUDError(responseObject[@"msg"]);
            [self pullUpFail];
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
        [self pullUpFail];
    }];
}

/**
 上拉失败，pagenum不增加
 */
- (void)pullUpFail{
    //上拉
    if (self.page > 1) {
        self.page --;
    }
    [self.tableview.mj_footer endRefreshing];
    [self.tableview.mj_header endRefreshing];
}


- (void)organizeData:(NSArray *)array{
    if (self.page == 1 && self.dataArray.count) {
        [self.dataArray removeAllObjects];
    }
    
    if (array.count == 0) {
        [self.tableview.mj_footer endRefreshingWithNoMoreData];
    }else{
        for (NSDictionary *dic in array) {
            [self.dataArray addObject:dic];
        }
        [self.tableview.mj_footer endRefreshing];
    }
    [self.tableview.mj_header endRefreshing];
    [self.tableview reloadData];
}



@end
